/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AppServices;

import Facades.LocationFacadeRemote;
import Facades.PlanFacadeRemote;
import Facades.ReserveFacadeRemote;
import VOs.Plan;
import VOs.Reserve;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Julian
 */
public class ReserveManage {

    static public ReserveFacadeRemote StartContextReserve() throws NamingException {
        InitialContext ctx = new InitialContext();
        ReserveFacadeRemote rbeanremote = (ReserveFacadeRemote) ctx.lookup("Facades.ReserveFacadeRemote");
        return rbeanremote;
    }

    static public LocationFacadeRemote StartContextLocation() throws NamingException {
        InitialContext ctx = new InitialContext();
        LocationFacadeRemote lbeanremote = (LocationFacadeRemote) ctx.lookup("Facades.LocationFacadeRemote");
        return lbeanremote;
    }

    static public PlanFacadeRemote StartContextPlan() throws NamingException {
        InitialContext ctx = new InitialContext();
        PlanFacadeRemote pbeanremote = (PlanFacadeRemote) ctx.lookup("Facades.PlanFacadeRemote");
        return pbeanremote;
    }

    static public boolean createReserve(int idplan, int idslocation, Date inicio, Date fin, String idext) throws Exception {
        boolean result = false;

        try {
            Reserve r = new Reserve();
            int id = StartContextReserve().count();
            System.out.println("anterior" + id);
            id++;
            System.out.println("actual" + id);
            r.setId(BigDecimal.valueOf(id));
            r.setInitialdate(inicio);
            r.setFinaldate(fin);
            r.setFkLocation(StartContextLocation().findid(idslocation));
            r.setFkPlan(StartContextPlan().findid(idplan));
            r.setExternalid(idext);
            r.setActivestate(BigInteger.ONE);

            StartContextReserve().create(r);

            result = true;


        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

    static public int TotalByPlan(int id) throws Exception {
        int result = 0;
        try {
            List<Reserve> r = StartContextReserve().findAll();
            ArrayList<Reserve> rfin = new ArrayList();

            for (int i = 0; i < r.size(); i++) {
                if (r.get(i).getFkPlan().getIdPlan().equals(BigDecimal.valueOf(id))) {
                    rfin.add(r.get(i));
                }

            }
            result = rfin.size();
            System.out.println(result);
            return result;//StartContextReserve().findidplan(p);

        } catch (Exception e) {
            throw e;
        }
    }




    static public List<Reserve> findByPlan(int id) throws Exception {

        try {
            List<Reserve> r = StartContextReserve().findAll();
            ArrayList<Reserve> rfin = new ArrayList();

            for (int i = 0; i < r.size(); i++) {
                //System.out.println(r.get(i).getFkPlan().getIdPlan().equals(BigDecimal.valueOf(id)));
                if (r.get(i).getFkPlan().getIdPlan().equals(BigDecimal.valueOf(id))&&r.get(i).getActivestate().equals(BigInteger.ONE)) {
                    System.out.println(r.get(i).getActivestate());
                    //if(r.get(i).getActivestate().equals(BigInteger.ONE))
                    rfin.add(r.get(i));
                }

            }
            return rfin;//StartContextReserve().findidplan(p);

        } catch (Exception e) {
            throw e;
        }
    }

    static public boolean editReserve(int idreserve, int idslocation, Date inicio, Date fin, String idext) throws Exception {
        boolean result = false;

        try {
            Reserve r = StartContextReserve().findid(idreserve);

            r.setInitialdate(inicio);
            r.setFinaldate(fin);
            r.setFkLocation(StartContextLocation().findid(idslocation));
            r.setExternalid(idext);

            StartContextReserve().edit(r);

            result = true;


        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

    static public boolean cancelReserve(int idreserva) throws Exception {
        boolean result = false;

        try {
            Reserve r = StartContextReserve().findid(idreserva);
            r.setActivestate(BigInteger.ZERO);
            StartContextReserve().edit(r);
            result = true;
        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

    public static List<Reserve> getActiveReserves() throws Exception
    {
        List<Reserve> activeReserveList = new ArrayList();
        List<Reserve> wholeList = null;
        Reserve currentReserve = null;
        BigDecimal compareParameter = BigDecimal.ONE;

        int i;

        try
        {
            wholeList = StartContextReserve().findAll();

            for (i=0;i<wholeList.size();i++)
            {
                currentReserve = wholeList.get(i);

                if (currentReserve.getActivestate().equals(compareParameter))
                {
                    activeReserveList.add(currentReserve);
                }
            }
        }
        catch (Exception e)
        {
            activeReserveList.clear();
            activeReserveList = null;
        }
        
        return activeReserveList;
    }
}
